package com.example.lock.aspect;

import com.alibaba.fastjson2.JSON;
import com.example.lock.annotation.ParamsTrace;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
 * <p>Description: </p>
 *
 * @author zg
 * @version v1.0.0
 * @since 2024/8/21
 **/
@Slf4j
@Aspect
@Component
public class ParamsTraceAspect {

    @Before("@annotation(paramsTrace)")
    public void before(JoinPoint point, ParamsTrace paramsTrace){
        String methodName = point.getSignature().getName();
        Object[] args = point.getArgs();
        log.info("Aspect->>>>>>>> method:{},is called on '{}', with args: {}", methodName, point.getThis(), JSON.toJSONString(args) );
    }

    @AfterReturning(value = "@annotation(paramsTrace)", returning = "returnObj")
    public void after(JoinPoint point, ParamsTrace paramsTrace, Object returnObj) {
        String methodName = point.getSignature().getName();
        log.info("Aspect->>>>>>>> method:{}, returns: {} ", methodName, JSON.toJSONString(returnObj));
    }

}
